package com.usermanage.controller;

import com.ordercore.Bean.OrderBean;
import com.ordercore.Common.OrderIOC;
import com.ordercore.Util.ResponseData.ResponseData;
import com.usermanage.SecurityConfig.SpringSecurityUtils;
import com.usermanage.SecurityConfig.UserDetail;
import com.usermanage.Service.CompanyService;
import com.usermanage.Service.ContractService;
import com.usermanage.Service.UserService;
import com.usermanage.beanParam.CompanyBean;
import com.usermanage.beanParam.UserBean;
import com.usermanage.util.HttpUtil;
import com.usermanage.util.JsonData;
import com.usermanage.util.StaticUtil;
import com.usermanage.util.StringUtil;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.servlet.ModelAndView;

import javax.annotation.Resource;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * SKU数据操作接口
 * searchQuota    根据组查询组下所有定额
 * addQuota   根据组id添加定额详情
 */

@RestController
@RequestMapping("/sales")
public class SalesOrderController {
    @Resource
    private UserService userService;

    @Resource
    private CompanyService companyService;

    @Resource
    private ContractService contractService;

    /**
     * 订单列表页
     *
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/orderList.page", method = RequestMethod.GET)
    public ModelAndView orderListPage() throws Exception {
        ModelAndView mv = new ModelAndView();
        mv.setViewName("/sales/orderList");
        return mv;
    }

    /**
     * 销售单列表接口
     *
     * @param orderNo   订单编号
     * @param userName  用户名
     * @param startTime 开始时间
     * @param endTime   结束时间
     * @param state     订单状态
     * @param page      页码
     * @param limit     每页数量
     * @return
     * @throws Exception
     */
    @RequestMapping(value = "/orderList.json", method = RequestMethod.POST)
    public JsonData orderList(String orderNo, String userName, String startTime, String endTime, String state, String page, String limit) throws Exception {
        if (StringUtils.isBlank(page)) {
            page = StaticUtil.pageIndex;
        }
        if (StringUtils.isBlank(limit)) {
            limit = StaticUtil.sizeCount;
        }
        UserDetail userDetail = SpringSecurityUtils.getCurrentUser();
        //当用户名不为空根据用户名模糊查询用户信息
        if (StringUtils.isNotBlank(userName)) {
            List<UserBean> userBeans = userService.findUserByUserNameAndCompanyId(userName, userDetail.getCompany_id());
            StringBuffer buffer = new StringBuffer();
            for (UserBean bean : userBeans) {
                buffer.append(bean.getId()).append(",");
            }
        }

        Map map = new HashMap();
        map.put("orderNo", orderNo);
        map.put("userGroup_id", "");
        map.put("user_id", "");
        map.put("type", "25");
        map.put("page", page);
        map.put("receiptCompany", userDetail.getCompany_id());//甲方企业id
        map.put("limit", limit);
        String param = StringUtil.ObjectToJson(map);
        ResponseData result = OrderIOC.queryOrderSummary(param);
        List<Map> list = null;
        int count = 0;
        if (result.isRet()) {
            String str = StringUtil.ObjectToJson(result.getData());
            list = StringUtil.JsonToList(str);
            for (Map resultMap : list) {
                UserBean bean = userService.findById(Integer.valueOf(resultMap.get("user_id").toString())).get();
                resultMap.put("user_name", bean.getUser_name());
                CompanyBean companyBean = companyService.findById(Integer.valueOf(userDetail.getCompany_id())).get();
                resultMap.put("company_name", companyBean.getCompany_name());
                resultMap.put("user_dept", bean.getUser_dept());
            }
            count = result.getCount();
        }
        Map resultMap = new HashMap();
        resultMap.put("list", list);
        resultMap.put("total", count == 0 ? 1 : count);
        resultMap.put("orderNo", orderNo);
        resultMap.put("userName", userName);
        resultMap.put("startTime", startTime);
        resultMap.put("endTime", endTime);
        resultMap.put("startTime", startTime);
        resultMap.put("state", state);
        resultMap.put("size", limit);
        return JsonData.success(resultMap);
    }

}
